<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" scroll :max-height="600">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="计划编号" prop="planCode">
        <el-input v-model="formData.planCode" placeholder="请输入计划编号" readonly />
      </el-form-item>
      <el-form-item label="航班号" prop="flightNo">
        <el-input v-model="formData.flightNo" placeholder="请输入航班号" readonly />
      </el-form-item>
      <el-form-item label="所属公司" prop="airlineComp">
        <el-select v-model="formData.airlineComp" placeholder="请选择所属航空公司">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PPLAN_AIRLINE_ID)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="起飞时间" prop="depTime">
        <el-time-picker
          v-model="formData.depTime"
          format="HH:mm"
          value-format="HH:mm"
          placeholder="选择起飞时间"
          clearable
          class="!w-240px"
          readonly
        />
      </el-form-item>
      <el-form-item label="起飞地点" prop="depLoc">
        <el-input v-model="formData.depLoc" placeholder="请输入起飞地点" readonly />
      </el-form-item>
      <el-form-item label="降落地点" prop="arrLoc">
        <el-input v-model="formData.arrLoc" placeholder="请输入降落地点" readonly />
      </el-form-item>
      <el-form-item label="降落时间" prop="arrTime">
        <el-time-picker
          v-model="formData.arrTime"
          format="HH:mm"
          value-format="HH:mm"
          placeholder="选择起飞时间"
          clearable
          class="!w-240px"
          readonly
        />
      </el-form-item>
      <el-form-item label="预落时间" prop="preLandingTime">
        <el-time-picker
          v-model="formData.preLandingTime"
          format="HH:mm"
          value-format="HH:mm"
          placeholder="选择起飞时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <!--<el-form-item label="航班性质" prop="flightType">-->
      <!--  <el-select v-model="formData.flightType" placeholder="请选择航班性质">-->
      <!--    <el-option label="请选择字典生成" value="" />-->
      <!--  </el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="航线类别" prop="airlineType">-->
      <!--  <el-select v-model="formData.airlineType" placeholder="请选择航线类别">-->
      <!--    <el-option label="请选择字典生成" value="" />-->
      <!--  </el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="前舱数量" prop="frontCabinQty">
        <el-input v-model="formData.frontCabinQty" placeholder="请输入前舱数量" />
      </el-form-item>
      <el-form-item label="中舱数量" prop="middleCabinQty">
        <el-input v-model="formData.middleCabinQty" placeholder="请输入中舱数量" />
      </el-form-item>
      <el-form-item label="后舱数量" prop="rearCabinQty">
        <el-input v-model="formData.rearCabinQty" placeholder="请输入后舱数量" />
      </el-form-item>
      <el-form-item label="机供品" prop="onboardId">
        <el-select
          placeholder="请选择机供品"
          filterable
          class="!w-240px"
          v-model="formData.onboardId"
        >
          <el-option
            v-for="simple in SimpleList1"
            :key="simple.id"
            :label="simple.planCode"
            :value="simple.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务用具" prop="serviceId">
        <el-select
          placeholder="请选择服务用具"
          filterable
          class="!w-240px"
          v-model="formData.serviceId"
        >
          <el-option
            v-for="simple in SimpleList2"
            :key="simple.id"
            :label="simple.planCode"
            :value="simple.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纺织品" prop="textileId">
        <el-select
          placeholder="请选择纺织品"
          filterable
          class="!w-240px"
          v-model="formData.textileId"
        >
          <el-option
            v-for="simple in SimpleList3"
            :key="simple.id"
            :label="simple.planCode"
            :value="simple.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="特殊说明" prop="remarks">
        <el-input
          type="textarea"
          :rows="2"
          v-model="formData.remarks"
          placeholder="请输入特殊说明"
        />
      </el-form-item>
      <el-form-item label="标记状态" prop="markStatus">
        <el-select v-model="formData.markStatus" placeholder="请选择生产状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.PPLAN_PRODUCE_MARK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择生产状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.PPLAN_PRODUCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { OnboardProducePlanApi, OnboardProducePlanVO } from '@/api/acm/onboardproduceplan'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { OnboardSupplyApi } from '@/api/acm/onboardsupply'
import { OnboardSupplyPlanApi } from '@/api/acm/onboardsupplyplan'
import { TablewareProducePlanApi } from '@/api/acm/tablewareproduceplan'
import { TextileProducePlanApi } from '@/api/acm/textileproduceplan'

/** 航机管理-机供品生产计划 表单 */
defineOptions({ name: 'OnboardProducePlanForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  planCode: undefined,
  flightNo: undefined,
  airlineComp: undefined,
  depTime: undefined,
  depLoc: undefined,
  depIata: undefined,
  arrLoc: undefined,
  arrIata: undefined,
  arrTime: undefined,
  preLandingTime: undefined,
  flightType: undefined,
  airlineType: undefined,
  frontCabinQty: undefined,
  middleCabinQty: undefined,
  rearCabinQty: undefined,
  onboardId: undefined,
  serviceId: undefined,
  textileId: undefined,
  remarks: undefined,
  markStatus: undefined,
  status: undefined
})
const formRules = reactive({
  flightNo: [{ required: true, message: '航班号不能为空', trigger: 'blur' }],
  markStatus: [{ required: true, message: '标记状态:1未标记;不能为空', trigger: 'blur' }],
  status: [
    { required: true, message: '生产状态，默认1未开始2进行中3已结束不能为空', trigger: 'change' }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OnboardProducePlanApi.getOnboardProducePlan(id)
      await getSimpleList1()
      await getSimpleList2()
      await getSimpleList3()
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 获取机供品精简列表 */
const SimpleList1 = ref([])
const getSimpleList1 = async () => {
  try {
    const res = await OnboardSupplyPlanApi.getOnboardSupplyPlanPageSimple({
      flightNo: formData.value.flightNo
    })
    console.log(res.list)
    SimpleList1.value = res.list
  } finally {
  }
}

/** 获取机供品精简列表 */
const SimpleList2 = ref([])
const getSimpleList2 = async () => {
  try {
    const res = await TablewareProducePlanApi.getTablewareProducePlanPageSimple({
      flightNo: formData.value.flightNo
    })
    console.log(res.list)
    SimpleList2.value = res.list
  } finally {
  }
}

/** 获取机供品精简列表 */
const SimpleList3 = ref([])
const getSimpleList3 = async () => {
  try {
    const res = await TextileProducePlanApi.getTextileProducePlanPageSimple({
      flightNo: formData.value.flightNo
    })
    console.log(res.list)
    SimpleList3.value = res.list
  } finally {
  }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OnboardProducePlanVO
    if (formType.value === 'create') {
      await OnboardProducePlanApi.createOnboardProducePlan(data)
      message.success(t('common.createSuccess'))
    } else {
      await OnboardProducePlanApi.updateOnboardProducePlan(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    planCode: undefined,
    flightNo: undefined,
    airlineComp: undefined,
    depTime: undefined,
    depLoc: undefined,
    depIata: undefined,
    arrLoc: undefined,
    arrIata: undefined,
    arrTime: undefined,
    preLandingTime: undefined,
    flightType: undefined,
    airlineType: undefined,
    frontCabinQty: undefined,
    middleCabinQty: undefined,
    rearCabinQty: undefined,
    onboardId: undefined,
    serviceId: undefined,
    textileId: undefined,
    remarks: undefined,
    markStatus: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
